package com.rao.crm.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.rao.crm.domain.BaseDict;
import com.rao.crm.service.BaseDictService;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;

import java.io.IOException;
import java.util.List;

/**
 * @author Srao
 * @version 1.0
 * @className BaseDictAction
 * @date 2019/9/19 16:27
 * @package com.rao.crm.web.action
 * @description
 */
public class BaseDictAction extends ActionSupport implements ModelDriven<BaseDict> {

    private BaseDict baseDict = new BaseDict();
    private BaseDictService baseDictService;
    @Override
    public BaseDict getModel() {
        return baseDict;
    }

    public void setBaseDictService(BaseDictService baseDictService) {
        this.baseDictService = baseDictService;
    }

    public String findByTypeCode() throws IOException {
        System.out.println("findByTypeCode执行了");
        /*获得该分类下的所有项*/
        List<BaseDict> list = baseDictService.findByTypeCode(baseDict.getDict_type_code());
        System.out.println(baseDict.getDict_type_code());
        /*把list转为json*/
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[] {"dict_sort", "dict_enable", "dict_memo"});
        JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
        System.out.println(jsonArray.toString());
        /*把jsonArray传到页面*/
        ServletActionContext.getResponse().setContentType("text/html; charset=utf-8");
        ServletActionContext.getResponse().getWriter().println(jsonArray.toString());
        return NONE;
    }



}
